from random import randint
import math

class constForDataSet:
    locRng = [7990, 8000] #100*100的二维空间
    TList = [100, 500, 1000, 1500]
    WList = [100, 500, 1000, 1500]
    rwList = [1.0, 1.5, 2.0, 2.5, 3.0]

    tmax = 1200
    per = 50

class CFDS(constForDataSet):
    pass

class baseGenerator:
    
	def gen(self, n):
		pass
        
class randomGenerator(baseGenerator):
    
    def __init__(self, mx):
        self.mx = mx

    def setMx(self, mx):
        self.mx = mx

    def gen(self, n):
        ret = [0] * n
        for i in range(n):
            x = randint(1, self.mx)
            ret[i] = x
        return ret

def genLoc(n, low=0, high=100):
    ret = []
    st = set()
    # for i in xrange(n):
    for i in range(n):
        while True:
            x = 1.0*randint(low*10**6, high*10**6) / 10**6
            y = 1.0*randint(low*10**6, high*10**6) / 10**6
            t = (x, y)
            if t not in st:
                break
        ret.append(t)
        st.add(t)
    return ret

def genTasks(n, wids, wlocs, rw):
    per = CFDS.per 
    tids = []
    tlocs = []
    for i in range(n):
        for j in range(per):
            tid = wids[i]
            if j>=int(per*0.7):
                # print((rw*1000000)*0.1, 0.10*(rw*10**6))
                tloc_x = wlocs[i][0] + 1.0*randint(-0.10*(rw*10**6), 0.10*(rw*10**6)) / 10**6
                tloc_y = wlocs[i][1] + 1.0*randint(-0.10*(rw*10**6), 0.10*(rw*10**6)) / 10**6
            else:
                tloc_x = wlocs[i][0] + 1.0*randint(-0.25*(rw*10**6), 0.25*(rw*10**6)) / 10**6
                tloc_y = wlocs[i][1] + 1.0*randint(-0.25*(rw*10**6), 0.25*(rw*10**6)) / 10**6
            tloc = (tloc_x, tloc_y)
            tids.append(tid)
            tlocs.append(tloc)
    return tids, tlocs

def sampleTasks(m, n, tids, tlocs):
    per = CFDS.per
    curPer = int( math.ceil(m*1.0/n) )
    _tids = []
    _tlocs = []
    for i in range(n):
        for j in range(curPer):
            if j>=int(curPer*0.7):
                k = i*per + int(per*0.7) + j - int(curPer*0.7)
            else:
                k = i*per + j
            tid, tloc = tids[k], tlocs[k]
            _tids.append(tid)
            _tlocs.append(tloc)
    return _tids[:m], _tlocs[:m]

def genWorkers(n):
    generator = randomGenerator(CFDS.tmax)
    wids = sorted(generator.gen(n))
    locs = genLoc(n, CFDS.locRng[0], CFDS.locRng[1])
    return wids, locs

def genDataSet():
    wmax = max(CFDS.WList)
    tmax = max(CFDS.TList)
    r = max(CFDS.rwList)
    wids,wlocs  = genWorkers(wmax)
    tids,tlocs = genTasks(wmax, wids, wlocs, r)
    for W in CFDS.WList:
        fileName = str(W)+"_"+str(W)+"_data.txt"
        _wids, _wlocs = wids[:W], wlocs[:W]
        _tids, _tlocs = sampleTasks(W, W, tids, tlocs)
        print(len(_wlocs))
        print(len(_tlocs))
        with open(fileName,"w",encoding="utf-8") as f:
            first = str(W)+" "+str(W)+" "+str(W+W)+"\n"
            f.write(first)
            for task in _tlocs:
                tstr = "t "+str(task[0])+" "+str(task[1])+"\n"
                f.write(tstr)
            for worker in _wlocs:
                wstr = "w "+str(worker[0])+" "+str(worker[1])+" "+str(r)+"\n"
                f.write(wstr)


if __name__  == "__main__":
      genDataSet()